package com.vfa.vocaburay;


import java.util.ArrayList;
import java.util.HashMap;
import com.vfa.test.AnswerE;
import com.vfa.test.QuestionData;
import com.vfa.test.QuestionE;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class FinalTest extends Activity {
	static int vitri = 0 ;
	public static int mark = 0;
	private TextView question;
	private RadioGroup mRadioGroup;
	private Button btPrePage;
	private Button btNextPage;
	private Button btSubmit;
	private TextView tvTimer;
	private RadioButton rbDapanA, rbDapanB, rbDapanC, rbDapanD;
	
	public static ArrayList<QuestionE> listQuestion;
	private ArrayList<AnswerE> listAnswer;
	int questionid;
	QuestionE questionE;
	QuestionData questiondata;
	String answer1, answer2, answer3, answer4;
	int rwa1, rwa2, rwa3, rwa4;
	HashMap<Integer, Integer> status = new HashMap<Integer, Integer>();
	ArrayList<Integer> listID = new ArrayList<Integer>();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.final_test);
		
		question = (TextView) findViewById(R.id.tvQuestion);
		btPrePage = (Button) findViewById(R.id.btPrePage);
		btNextPage = (Button) findViewById(R.id.btNextPage);
		btSubmit = (Button) findViewById(R.id.btSubmit);
		mRadioGroup = (RadioGroup) findViewById(R.id.rdgAnswer);
		rbDapanA = (RadioButton) findViewById(R.id.rdbDapanA);
		rbDapanB = (RadioButton) findViewById(R.id.rdbDapanB);
		rbDapanC = (RadioButton) findViewById(R.id.rdbDapanC);
		rbDapanD = (RadioButton) findViewById(R.id.rdbDapanD);
		tvTimer = (TextView) findViewById(R.id.tvTimer);
		
		listQuestion = new ArrayList<QuestionE>();
		questiondata = new QuestionData(this);
		
		btPrePage.setEnabled(false);
		btSubmit.setEnabled(false);
		mRadioGroup.clearCheck();
		
		Xuat_cau_hoi(FinalTest.vitri);
		Xuat_cau_tra_loi(questionid);
		
		btNextPage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(vitri < listQuestion.size())
				{	
				SaveStatus();
				vitri++;
				Xuat_cau_hoi(vitri);
				Xuat_cau_tra_loi(questionid);
				}
				if(vitri > 0)
					btPrePage.setEnabled(true);
				if(vitri == listQuestion.size()-1){
					btNextPage.setEnabled(false);
					btSubmit.setEnabled(true);
				}
				mRadioGroup.clearCheck();
				
			}
		});
		
		btPrePage.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				vitri--;
				Xuat_cau_hoi(vitri);
				Xuat_cau_tra_loi(questionid);
				if(vitri == 0){
					btPrePage.setEnabled(false);
				}
				if(vitri < listQuestion.size()){
					btNextPage.setEnabled(true);
				}
				Log.i("Vitri",String.valueOf(vitri) );
			}
		});
		
		btSubmit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(FinalTest.this);
				builder.setMessage("Are you sure you want to finish the test?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						SaveStatus();
						Intent i =new Intent(FinalTest.this, ResultTest.class);
						startActivity(i);
						finish();
					}
				});
				builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}).show();
				
			}
		});
		
	}
	
	
	
	@Override
	protected void onStart() {
		super.onStart();
		final MyCounter timer = new MyCounter (90000, 1000);
		//timer.start();
	}
	
	private void Xuat_cau_hoi(int vitri){
		listQuestion = QuestionData.getQuestion(0);
		String strquestion = listQuestion.get(vitri).getQuestion();
		question.setText(strquestion);
		questionid = listQuestion.get(vitri).getQuestionId();
	}
		
	private void Xuat_cau_tra_loi(int idquestion){
		listAnswer = new ArrayList<AnswerE>();
		listAnswer = QuestionData.getAnswer(idquestion);
		answer1 = listAnswer.get(0).getAnswer();
		answer2 = listAnswer.get(1).getAnswer();
		/*answer3 = listAnswer.get(2).getAnswer();
		answer4 = listAnswer.get(3).getAnswer();*/
		rwa1 = listAnswer.get(0).getRiro();
		rwa2 = listAnswer.get(1).getRiro();
		/*rwa3 = listAnswer.get(2).getRiro();
		rwa4 = listAnswer.get(3).getRiro();*/
		rbDapanA.setText(answer1);
		rbDapanB.setText(answer2);
		rbDapanC.setText(answer3);
		rbDapanD.setText(answer4);
	}
	
	public int returnchecked(){
		int isCheck = 0;
		if (rbDapanA.isChecked()) {
			return 1;
		}
		if (rbDapanB.isChecked()) {
			return 2;
		}
		if (rbDapanC.isChecked()) {
			return 3;
		}
		if (rbDapanD.isChecked()) {
			return 4;
		}
		
		return isCheck;
	}
	
	private void SaveStatus(){
		listID.add(questionid);
		status.put(questionid, returnchecked());	
	}
	
	
	
	
	public class MyCounter extends CountDownTimer{

		public MyCounter(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
			
		}

		@Override
		public void onFinish() {
			tvTimer.setText("Time Compeleted");
			SaveStatus();
			Intent i =new Intent(FinalTest.this, ResultTest.class);
			startActivity(i);
			finish();
		}
		@Override
		public void onTick(long millisUntilFinished) {
			tvTimer.setText(millisUntilFinished/1000+"");
		}	
	}
}
